class Promise2 {
    start = 'panding'
    success = null
    fail = null

    resolve(result) {
        setTimeout(() => {
            this.start = 'fulfilled'
            this.success(result)
        })
    }

    reject(err) {
       setTimeout(() => {
           this.start = 'rejected'
           this.fail(err)
       })
    }

    constructor(fn) {
        fn(this.resolve.bind(this), this.reject.bind(this))
    }

    then(success, fail) {
        this.success = success
        this.fail = fail
    }
}






const getWeather = city => new Promise2((resolve, reject) => {
    let xhr = new XMLHttpRequest()
    let url = 'http://rap2.taobao.org:38080/app/mock/245421/getWeather?city=' + city
    xhr.open('GET', url, true)
    xhr.onload = () => {
        if(xhr.status === 200) {
            resolve(JSON.parse(xhr.responseText))
        } else {
            reject('获取${city}天气失败')
        }
    }
    xhr.send()
})
getWeather('北京').then(data => {
    console.log(data)
}, err => {
    console.log(err)
})